package com.qiantu.phone.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.BeforeAfterTimesBean;
import com.qiantu.api.entity.TimesBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.action.StatusAction;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.app.AppFragment;
import com.qiantu.phone.ui.activity.AddConditionActivity;
import com.qiantu.phone.ui.activity.EditTimeActivity;
import com.qiantu.phone.ui.activity.TimeActivity;
import com.qiantu.phone.ui.adapter.TimesAdapterNew;
import com.qiantu.phone.widget.StatusLayout;
import com.qiantu.phone.widget.StickyItemDecoration;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * time   : 2021/12/22
 */
public final class TimeFragmentNew extends AppFragment<AppActivity>
        implements OnRefreshListener {

    private int afterPage = 1;
    private int beforePage = 1;
    private int beforePageSize = 10;
    //1初始化  2加载更早之前的
    private int requestType = 1;
    private List<TimesBean> firstPageRemainTimes;

    public static TimeFragmentNew newInstance() {
        TimeFragmentNew fragment = new TimeFragmentNew();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    private StatusLayout mStatusLayout;
    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView mRecyclerView;
    private LinearLayout ll_Noequipment;
    private View btnAdd;
    private TimesAdapterNew mTimesAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_time;
    }

    @Override
    protected void initView() {
        mStatusLayout = findViewById(R.id.status_layout);
        smartRefreshLayout = findViewById(R.id.smart_refresh_layout);
        mRecyclerView = findViewById(R.id.recycler_view);
        ll_Noequipment = findViewById(R.id.ll_Noequipment);
        btnAdd = findViewById(R.id.btn);
        mLinearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mTimesAdapter = new TimesAdapterNew(getAttachActivity(), ((TimeActivity)getActivity()).getViewPager());
        mRecyclerView.setAdapter(mTimesAdapter);
        mRecyclerView.addItemDecoration(new StickyItemDecoration());
        smartRefreshLayout.setEnableLoadMore(false);
        smartRefreshLayout.setOnRefreshListener(this);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(EditTimeActivity.class);
            }
        });
//        smartRefreshLayout.setRefreshHeader(new RefreshHeaderWrapper())
//        showLoading();
    }

    @Override
    protected void initData() {

    }

    private void getFirstData(){
        requestType = 1;
        beforePage = 1;
        getTimes();
    }

    @Override
    public void onResume() {
        super.onResume();
        if(!isHidden()){
            getFirstData();
        }
    }

    private void getTimes() {
        String houseSerialNo = AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo();
        //获取通知消息
        LLHttpManager.getTimes(this, beforePage, beforePageSize, requestType, houseSerialNo, new HttpCallback<HttpData<BeforeAfterTimesBean>>(null) {
            @Override
            public void onSucceed(HttpData<BeforeAfterTimesBean> result) {
                if (result != null && result.getData() != null && (
                        (result.getData().getBeforeTimeRecords() != null && result.getData().getBeforeTimeRecords().size() > 0)
                            || (result.getData().getAfterTimeRecords() != null && result.getData().getAfterTimeRecords().size() > 0))) {
                    List<TimesBean> beforeTimes = result.getData().getBeforeTimeRecords();
                    switch (requestType) {
                        case 1:
                            //首次初始化
//                            List<TimesBean> times = new ArrayList<>();
                            List<TimesBean> beforeData = new ArrayList<>();
                            if (beforeTimes != null) {
                                List<TimesBean> firstPageLastTwoTimes = new ArrayList<>();
                                if (beforeTimes.size() > 1) {
                                    //保存最后2条
                                    firstPageLastTwoTimes.add(beforeTimes.get(beforeTimes.size() - 2));
                                    firstPageLastTwoTimes.add(beforeTimes.get(beforeTimes.size() - 1));
                                    //删除最后2条后保存
                                    beforeTimes.remove(beforeTimes.size() - 1);
                                    beforeTimes.remove(beforeTimes.size() - 1);
                                    firstPageRemainTimes = beforeTimes;
                                    beforeData = firstPageLastTwoTimes;
                                }else{
                                    beforeData = beforeTimes;
                                }
                            }
                            List<TimesBean> afterData = new ArrayList<>();
                            if (result.getData().getAfterTimeRecords() != null) {
                                afterData = result.getData().getAfterTimeRecords();
                            }
                            mTimesAdapter.setData(beforeData, afterData);
                            smartRefreshLayout.setVisibility(View.VISIBLE);
                            ll_Noequipment.setVisibility(View.GONE);
                            break;
                        case 2:
                            int addSize = 0;
                            if (beforePage == 2 && firstPageRemainTimes != null && firstPageRemainTimes.size() > 0) {
                                addSize = firstPageRemainTimes.size();
                                mTimesAdapter.addBeforeData(firstPageRemainTimes);
                                firstPageRemainTimes.clear();
                                firstPageRemainTimes = null;
                            }
                            if (beforeTimes != null) {
                                addSize += beforeTimes.size();
                                mTimesAdapter.addBeforeData(beforeTimes);
                            }
                            mTimesAdapter.notifyDatasetChanged();
                            mLinearLayoutManager.scrollToPositionWithOffset(addSize, 0);
                            break;
                    }
                }else{
                    if(requestType == 1){
                        smartRefreshLayout.setVisibility(View.GONE);
                        ll_Noequipment.setVisibility(View.VISIBLE);
//                        showEmpty(R.attr.img_emptystate_notime, R.string.no_smart_task, R.string.add, new StatusLayout.OnBtnClickListener() {
//                            @Override
//                            public void onClick(StatusLayout layout) {
//                                Intent intent = new Intent(getContext(), AddConditionActivity.class);
//                                intent.putExtra("type", AddConditionActivity.TYPE_SCENE_LINKAGE);
//                                startActivity(intent);
//                            }
//                        });
                    }
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
//                showError(R.attr.img_emptystate_notime, new StatusLayout.OnBtnClickListener() {
//                    @Override
//                    public void onClick(StatusLayout layout) {
//
//                    }
//                });
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
                switch (requestType) {
                    case 2:
                        smartRefreshLayout.finishRefresh();
                        break;
                }
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        requestType = 2;
        beforePage = beforePage + 1;
        getTimes();
    }

//    @Override
//    public StatusLayout getStatusLayout() {
//        return mStatusLayout;
//    }
}